﻿using System;
using System.Collections;
using System.Configuration;

using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;

namespace Exercise
{
    class Program
    {

        static void Main(string[] args)
        {
            Test();

            System.Console.WriteLine("it's over");
            Console.ReadLine();
        }

        public static void Test()
        {
            #region//练习xpath
            //StringBuilder txt = new StringBuilder(127);
            //txt.Append(" <Root>")
            //    .Append("  <Ask ID=\"725550\" Name=\"想知道一下台湾省的火山分布，越详细越好哦\" />")
            //     .Append("  <Ask ID=\"725550\" Name=\"想知道一下台湾省的火山分布，越详细越好哦\" />")
            //    .Append("</Root>");
            //XmlDocument xdoc = new XmlDocument();
            //xdoc.LoadXml(txt.ToString());
            //XmlNodeList list = xdoc.SelectNodes("/Root/Ask");
            //foreach (XmlNode n in list) {
            //    Console.WriteLine(n.Name);
            //    foreach (XmlAttribute xa in n.Attributes)
            //    {
            //        Console.WriteLine("{0}={1}",xa.Name,xa.InnerText);
            //    }
            //}
            #endregion

            #region//练习linq
            //Student s1 = new Student(1, "xue");
            //Student s2 = new Student(2, "sun");
            //Student s3 = new Student(3, "jin");
            //Student s4 = new Student(4, "hu");

            //List<Student> stus = new List<Student>();
            //stus.Add(s1);
            //stus.Add(s2);
            //stus.Add(s3);
            //stus.Add(s4);

            //List<int> ids = new List<int>();
            //ids.Add(3);
            //ids.Add(4);
            //ids.Add(1);
            //ids.Add(2);

            //var result = ids.Select((id, index) => new { id = id, index = index }).Join(stus, i => i.id, s => s.Id, (i, s) =>s).ToList();
            //foreach (var i in result)
            //{
            //    Console.WriteLine(String.Format("Id={0} Name={1}",i.Id,i.Name));
            //}
            #endregion

            #region//练习Entity Framework 4.0
            //CmsEntities ce = new CmsEntities();
            //cms_news news = new cms_news();
            //news.CreateUserId = 1;
            //news.CreateDate = DateTime.Now;
            //news.ModiDate = DateTime.Now;
            //news.NewsState = 1;
            //ce.cms_news.AddObject(news);
            //int i = ce.SaveChanges(System.Data.Objects.SaveOptions.DetectChangesBeforeSave);
            //Console.WriteLine(i);
            //Console.WriteLine(news.NewsId);
            #endregion

            #region//练习Entity Framework 4.1
            //CmsArticle ca = new CmsArticle();
            ////ICollection<Cms_Article> list = ca.FindList();
            //ICollection<Cms_Article> list = ca.FindList("select ArticleId, NewsType, CreateUserId, CreateDate, AuthorName, NewsSource, ModiUserId, ModiDate From cms_article as a where a.articleid=1 ");
            //foreach (Cms_Article m in list)
            //{
            //    Console.WriteLine(m.AuthorName);
            //}
            #endregion

            #region//练习  随机插入1~100到一个长度为100的数组中
            //List<int> lst = new List<int>();
            //int[] result = new int[100];
            //for (int x = 1; x < 101;x++)
            //{
            //    lst.Add(x);
            //}

            //int i = 0;
            //Random r = new Random();
            //while(lst.Count>0)
            //{
            //    int x = r.Next(0, lst.Count);
            //    result[i] = lst[x];
            //    i++;
            //    lst.RemoveAt(x);
            //}
            //Console.WriteLine("排序前...");
            //foreach (int x in result)
            //{
            //    Console.Write(x+",");
            //}
            #endregion

            #region//练习字符串替换
            //String input = "ww xxx  yyy   zzz    5";
            //Console.WriteLine(String.Format("替换前:{0}", input));
            //input = System.Text.RegularExpressions.Regex.Replace(input, "[ 　]{2,}", " ");
            //Console.WriteLine(String.Format("替换后:{0}",input));
            #endregion

            #region//练习数组和字符
            //String str = "T";

            //char c = str[0];
            //char[] cs = str.ToArray();

            //char copyc = c;
            //char[] copyArray = cs;

            //c = 'O';
            //cs[0] = 'O';

            //Console.WriteLine(copyc + " - " + copyArray[0]);

            #endregion

            #region//斐波那契数列
            //int i = Foo(8);
            //Console.WriteLine(i);
            #endregion

            #region//yield关键字
            //List<int> lst = Power(2, 15).ToList<int>();
            //foreach(var i in lst)
            //Console.Write("{0},", i);
            //Console.WriteLine(System.Environment.CommandLine);
            #endregion

            #region//singleton
            //decimal d = 49.1m;
            //Console.WriteLine(d>50&&d<51?(int)d:d<50&&d>49?(int)Math.Ceiling(d):50);
            //Earth.GetInstance();
            #endregion

            #region//
           
            #endregion
        }

        /// <summary>
        /// 求一个数的N次幂
        /// </summary>
        /// <param name="number"></param>
        /// <param name="exponent"></param>
        /// <returns></returns>
        private static IEnumerable<int> Power(int number, int exponent)
        {
            int result = 1;
            int i = 0;
            while (i++ < exponent)
            {
                yield return result *= number;
            }
        }
        static int Foo(int i)
        {
            if (i <= 0)
            {
                return 0;
            }
            else if (i <= 2)
            {
                return 1;
            }
            else
            {
                return Foo(i - 1) + Foo(i - 2);
            }
        }
    }
    public class Ask { }
    public class Student
    {
        public int Id { set; get; }
        public String Name { set; get; }
        public Student(int i, String name)
        {
            Id = i;
            Name = name;
        }
    }
    public sealed class Earth
    {
        private static volatile Earth instance = new Earth();
        private Earth()
        {
            Console.WriteLine("constructor");
        }
        public static Earth GetInstance()
        {
            Console.WriteLine("return earth instance;");
            return instance;
        }
    }
    public class Singleton
    {
        private Object locker = new Object();
        private Singleton me = null;
        private Singleton()
        {
        }
        public Singleton Instance
        {
            get
            {
                if (me == null)
                {
                    lock (locker)
                    {
                        if (me == null)
                        {
                            me = new Singleton();
                        }
                    }
                }
                return me;
            }
        }

    }

}
